package com.whale.controller.admin;




import com.whale.constant.MessageConstant;
import com.whale.constant.StatusConstant;
import com.whale.dto.GoodsPageQueryDTO;
import com.whale.entity.Goods;
import com.whale.result.PageResult;
import com.whale.result.ResponseResult;
import com.whale.service.GoodsService;
import com.whale.vo.GoodsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜品管理
 */
@RestController("adminGoodsController")
@RequestMapping("/admin/goods")
@Api(tags="商品管理相关接口")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("添加商品")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody Goods goods){
        return goodsService.addGoods(goods);
    }

    @ApiOperation("修改商品")
    @PutMapping("/update")
    public ResponseResult update(@RequestBody Goods goods){
         goodsService.updateById(goods);
         return ResponseResult.okResult(1, MessageConstant.GOODS_UPDATE_SUCCESS);
    }

    @ApiOperation("商品分页查询")
    @GetMapping("/pageQuery")
    public ResponseResult page(@RequestBody GoodsPageQueryDTO goodsPageQueryDTO){
        PageResult pageResult=goodsService.pageQuery(goodsPageQueryDTO);
        return ResponseResult.okResult(pageResult);
    }

    @ApiOperation("查询商品详细")
    @GetMapping("/detail/{id}")
    public ResponseResult getGoodsDetails(@PathVariable Long id){
        Goods goods=goodsService.getById(id);
        GoodsVO goodsVO=new GoodsVO();
        BeanUtils.copyProperties(goods,goodsVO);
        return ResponseResult.okResult(goodsVO);
    }

    @DeleteMapping("/bitchDelete")
    @ApiOperation("商品批量删除")
    public ResponseResult deleteByIds(@RequestParam List<Long> ids){
        goodsService.removeByIds(ids);
        return ResponseResult.okResult(1,MessageConstant.GOODS_DELETE_SUCCESS);
    }

    @ApiOperation("起售或停售商品")
    @PostMapping("/startOrStop/{id}")
    public ResponseResult startOrStop(@PathVariable Long id){
        Goods goods=goodsService.getById(id);
        if(goods.getStatus().equals(StatusConstant.ENABLE)){
            goods.setStatus(StatusConstant.DISABLE);
        } else {
            goods.setStatus(StatusConstant.ENABLE);
        }
        goodsService.updateById(goods);
        return ResponseResult.okResult();
    }


}
